<?php

namespace App\Module\Admin\Controllers;

use App\Module\Admin\Validator\GoodsValidator;
use App\System\Base\Repositories\Repository;
use App\System\Base\Services\ImageService;
use App\System\Cms\Repositories\CmsTopicAlbumRepository;
use App\System\Cms\Repositories\CmsTopicRepository;
use App\System\Goods\Repositories\GoodsAlbumRepository;
use App\System\Goods\Repositories\GoodsInfoRepository;
use Illuminate\Http\Request;

/**
 * Class GoodsController 商品管理
 * @package App\Module\Admin\Controllers
 */
class GoodsController extends Controller
{
    /**
     * 商品管理
     */

    //商品列表
    public function index(Request $request)
    {
        $keyword = $request['keyword']??'';
        $type = $request['type']??'';
        $status = $request['status']??'';
        $page = $request['page']??'1';
        $where = ['keyword'=>$keyword,'type'=>$type,'status'=>$status,'page'=>$page];
        $goods = GoodsInfoRepository::getInstance();
        $data = $goods->getList($keyword,$status,$type);
        return view('admin.goods.index',compact('data','where'));
    }

    //新增商品
    public function add(Request $request)
    {
        return view('admin.goods.add');
    }

    //编辑商品
    public function edit(Request $request)
    {
        $id = $request['id'];
        $goods = GoodsInfoRepository::getInstance();
        $data = $goods->getDetail($id);
        $album = GoodsAlbumRepository::getInstance();
        $imgData = $album->getList($id,true);
        return view('admin.goods.edit',compact('data','imgData'));
    }

    // 修改商品状态
    public function ajaxChangeSt(Request $request)
    {
        if ($request->isMethod('post')) {
            $thepost = $request->only('id','status', 'delete');
            $id = $thepost['id'];
            $goods = GoodsInfoRepository::getInstance();
            if(isset($thepost['status'])){
                $updata['status'] = $thepost['status']==1?2:1;
                $res = $goods->update($id,$updata);
            }else{
                $res = $goods->deleteData($id);
            }
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => '']);
        }
    }

    //页面图片上传
    public function uploadFiles(Request $request)
    {
        $file = $_FILES;
        $image = ImageService::getInstance();
        $result = $image->uploadFilebak($file,$request);
        echo json_encode($result);die;
    }

    //保存商品信息
    public function ajaxSave(Request $request)
    {
        if($request->isMethod('post')){
            (new GoodsValidator())->check();
            $thepost = $request->only('id','name','decription','cost_price','stock','key_word','content','img_ids','order','member_price');
            $id = $thepost['id']??'';
            $updata['name'] = $thepost['name']??'';
            $updata['decription'] = $thepost['decription']??'';
            $updata['cost_price'] = $thepost['cost_price']??'';
            $updata['stock'] = $thepost['stock']??'';
            $updata['key_word'] = $thepost['key_word']??'';
            $updata['content'] = $thepost['content']??'';
            $updata['member_price'] = $thepost['member_price']??0;
            $imgData = [];
            if(isset($thepost['img_ids'])){
                foreach ($thepost['img_ids'] as $k=>$v){
                    if(!$thepost['order'][$k]){
                        return response()->json(['message' => '未填写图片排序', 'status' => 'FAIL', 'url' => '']);
                    }
                    $imgData[$k]['image_id'] = $v;
                    $imgData[$k]['sort'] = $thepost['order'][$k];
                }
            }else{
                return response()->json(['message' => '未上传图片', 'status' => 'FAIL', 'url' => '']);
            }
            $goods = GoodsInfoRepository::getInstance();
            if($id){
                $res = $goods->updateData($id,$updata,$imgData);
            }else{
                $updata['market_price'] = 0;
                $updata['image_id'] = 0;
                $updata['sales_volume'] = 0;
                $updata['created_at'] = date('Y-m-d H:i:s',time());
                $res = $goods->addData($updata,$imgData);
            }
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            $url = !empty($res['data']) ? url('/admin/goods') : '';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
        }
    }


}
